package com.winsdom.controller;

import com.winsdom.anno.Authentication;
import com.winsdom.annotation.OperationLog;
import com.winsdom.dto.CategoryTreeNodes;
import com.winsdom.dto.resource.ChageTablesDto;
import com.winsdom.dto.resource.PayFeesRecordDto;
import com.winsdom.result.PageResult;
import com.winsdom.result.Result;
import com.winsdom.service.IResourceService;
import com.winsdom.vo.ResourceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.winsdom.dto.*;
import javax.validation.Valid;
import java.util.*;

import static com.winsdom.enums.AuthenticationType.RESOURCE_MANAGEMENT;

@RestController
@RequestMapping("/resource")
@Api(tags = "资源管理相关接口")
@Authentication(value = RESOURCE_MANAGEMENT)
public class ResourceController {

    @Autowired
    private IResourceService resourceService;

    @GetMapping("/get/three_tables/{categoryId}")
    @ApiOperation("获取该资源下（根据分类ID（资源））的三表的信息 ")
    @OperationLog(description = "获取该资源下（根据分类ID（资源））的三表的信息")
    public Result getThreeTables(@PathVariable("categoryId") Long categoryId) {
        return resourceService.getThreeTables(categoryId);
    }

    @GetMapping("/get/three_tables/status/{id}")
    @ApiOperation("根据三表ID停用表")
    @OperationLog(description = "根据三表ID停用表")
    public Result stopThreeTables(@PathVariable("id") Integer id) {
        return resourceService.stopThreeTables(id);
    }

    @GetMapping("/get/three_tables/{id}/resourceName")
    @ApiOperation("换表前根据表ID获取所属资源")
    @OperationLog(description = "换表前根据表ID获取所属资源")
    public Result getResourceName(@PathVariable("id") Integer id) {
        return resourceService.getResourceName(id);
    }

    @PostMapping("/change-table")
    @ApiOperation("提交换表")
    @OperationLog(description = "提交换表")
    public Result changeTable(@RequestBody @Valid ChageTablesDto chageTablesDto) {
        return resourceService.changeTable(chageTablesDto);
    }

    @PostMapping("/PayFeesRecord")
    @ApiOperation("列表显示当前资源已关联的客户的计费及欠款信息")
    @OperationLog(description = "列表显示当前资源已关联的客户的计费及欠款信息")
    public Result PayFeesRecord(@RequestBody PayFeesRecordDto payFeesRecordDto) {
        Result result = resourceService.PayFeesRecord(payFeesRecordDto);
        return result;
    }
    /*
     * 获取组织树
     */
    @GetMapping("/organizationTree")
    @Operation(summary = "当前账号下已授权的楼盘的组织树", description = "当前账号下已授权的楼盘的组织树（先登录，自动获取账号id）")
    @OperationLog(description = "获取当前账号下已授权的楼盘的组织树")
    public Result getOrganizationTree() {
        List<CategoryTreeNodes> organizationTree = resourceService.getOrganizationTree();
        return Result.success(organizationTree);
    }

    /*
     * 查询该组织下所有资源
     */
    @PostMapping("/resourceList/{id}")
    @ApiOperation("分页查询当前组织下所有资源信息")
    @ApiImplicitParam(name = "Id", value = "当前资源ID", required = true, dataType = "Long", paramType = "path")
    public PageResult getResourceList(@PathVariable Long id,@RequestBody ResourceFilterRequest request) {
        PageResult resourceList = resourceService.getResourceList(id, request);
        return resourceList;
    }

    /*
     * 新增资源
     */
    @PostMapping("/addResource")
    @ApiOperation("新增资源")
    public Result addResource(@RequestBody ResourceDTO resourceDTO) {
        resourceService.addResource(resourceDTO);
        return Result.success("新增成功");
    }

    /*
     * 查找单个资源详细信息
     */
    @GetMapping("/getResourceById/{id}")
    @ApiOperation("查询资源详细信息")
    @ApiImplicitParam(name = "id", value = "资源ID", required = true, dataType = "Long", paramType = "path")
    public Result getResourceById(@PathVariable Long id) {
        ResourceVO resource = resourceService.getResourceById(id);
        return Result.success(resource);
    }
    /*
     * 修改资源信息
     */
    @PostMapping
    @ApiOperation("修改资源信息")
    public Result updateResource(@RequestBody ResourceUpdDTO resourceUpdDTO){
        resourceService.update(resourceUpdDTO);
        return Result.success("修改成功");
    }
    /*
     * 删除资源信息
     */
    @GetMapping("/{id}")
    @ApiOperation("删除资源信息")
    @ApiImplicitParam(name = "id", value = "资源ID", required = true, dataType = "Long", paramType = "path")
    public Result deleteResource(@PathVariable Long id){
        resourceService.deleteById(id);
        return Result.success("删除成功");
    }
    /*
     * 拆分资源
     */
    @PostMapping("/splitResource/{id}")
    @ApiOperation(value = "拆分资源")
    @ApiImplicitParam(name = "id", value = "被拆分资源ID", required = true, dataType = "Long", paramType = "path")
    public Result splitResource(@PathVariable("id") Long id,@RequestBody ResourceSplitDTO resourceSplitDTO){
        resourceService.splitResource(id,resourceSplitDTO);
        return Result.success("拆分成功");
    }
    /*
     * 查询被合并资源同层的资源
     * @param id 被合并资源的id
     */
    @GetMapping("/getCommonResource/{id}")
    @ApiOperation(value = "查询被合并资源同层的资源")
    @ApiImplicitParam(name = "id", value = "被合并资源的ID", required = true, dataType = "Long", paramType = "path")
    public Result getCommonResource(@PathVariable("id") Long id){
        return Result.success(resourceService.getCommonResource(id));
    }
    /*
     * 合并资源
     */
    @PostMapping("/mergeResource")
    @ApiOperation(value = "合并资源")
    public Result mergeResource(@RequestBody MergeResourceDTO mergeResourceDTO){
        resourceService.mergeResource(mergeResourceDTO);
        return Result.success("合并成功");
    }
}
